###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# ControlPanel/Output/ModifyLevel.pm.pl -> ModifyLevel module                 #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package Skin::CP::ModifyLevel;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Standard.pm.pl";

use HTML::Dialog;
use HTML::Form;
use strict;

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless ($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = (input => undef, error => "", @_);

  my $LANGUAGE = {
    "MISSING" => {
      "TITLE"         => qq~<li>You didn't fill in the "Title" field.~
    },
    "TOOLONG" => {
      "TITLE"         => qq~<li>The value for the "Title" field must be 256 characters or less.~
    },
    "ERROR"         => qq~<p><font class="error">There were errors:<ul>[%error%]</ul></font>~
  };

  $in{'error'} = &Standard::ProcessError(LANGUAGE => $LANGUAGE, ERROR => $in{'error'});

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Dialog = HTML::Dialog->new();
  my $Form   = HTML::Form->new();

  my $Body = $Dialog->TableRow(items => [{ body => "Please fill out all the required (<font class=\"form-required\"><b>*</b></font>) fields:".$in{'error'}, colspan => "2" }]);

  $Body .= $Dialog->FormTextBox(
    value     => $in{'input'}->{'LEVEL'}->{'ID'},
    subject   => "ID"
  );
  
  $Body .= $Dialog->FormTextBox(
    name      => "FORM_TITLE",
    value     => $in{'input'}->{'LEVEL'}->{'TITLE'},
    subject   => "Title",
    required  => 1
  );

  $Body .= $Dialog->FormCheckBox(
    name      => "FORM_SUPERUSER",
    value     => $in{'input'}->{'LEVEL'}->{'permissions'}->{'SUPERUSER'},
    items     => [{ value => "1", label => "Superuser (can do everything)?" }]
  );

  my @General = (
    { name => "SYSTEMOPTIONS", label => "View/Modify system options?" },
    { name => "GENERALOPTIONS", label => "View/Modify general options?" },
    { name => "EXTRAFIELDS_VIEW", label => "View extra resource fields?" },
    { name => "EXTRAFIELDS_CREATE", label => "Create extra resource fields?" },
    { name => "EXTRAFIELDS_MODIFY", label => "Modify extra resource fields?" },
    { name => "EXTRAFIELDS_REMOVE", label => "Remove extra resource fields?" },
    { name => "CATEGORIES_VIEW", label => "View categories?" },
    { name => "CATEGORIES_CREATE", label => "Create categories?" },
    { name => "CATEGORIES_MODIFY", label => "Modify categories?" },
    { name => "CATEGORIES_REMOVE", label => "Remove categories?" },
    { name => "RESOURCES_VIEW", label => "View resources?" },
    { name => "RESOURCES_APPROVE", label => "Approve resources?" },
    { name => "RESOURCES_CREATE", label => "Create resources?" },
    { name => "RESOURCES_MODIFY", label => "Modify resources?" },
    { name => "RESOURCES_REMOVE", label => "Remove resources?" },
    { name => "REVIEWS_VIEW", label => "View reviews?" },
    { name => "REVIEWS_MODIFY", label => "Modify reviews?" },
    { name => "REVIEWS_REMOVE", label => "Remove reviews?" },
    { name => "ADMINS_VIEW", label => "View admins?" },
    { name => "ADMINS_CREATE", label => "Create admins?" },
    { name => "ADMINS_MODIFY", label => "Modify admins?" },
    { name => "ADMINS_REMOVE", label => "Remove admins?" },
    { name => "LEVELS_VIEW", label => "View levels?" },
    { name => "LEVELS_CREATE", label => "Create levels?" },
    { name => "LEVELS_MODIFY", label => "Modify levels?" },
    { name => "LEVELS_REMOVE", label => "Remove levels?" },
    { name => "SKINS_VIEW", label => "View skins?" },
    { name => "SKINS_CREATE", label => "Create skins?" },
    { name => "SKINS_MODIFY", label => "Modify skins?" },
    { name => "SKINS_REMOVE", label => "Remove skins?" },
    { name => "STYLESHEETS_VIEW", label => "View stylesheets?" },
    { name => "STYLESHEETS_CREATE", label => "Create stylesheets?" },
    { name => "STYLESHEETS_MODIFY", label => "Modify stylesheets?" },
    { name => "STYLESHEETS_REMOVE", label => "Remove stylesheets?" },
    { name => "LANGUAGESETS_VIEW", label => "View language sets?" },
    { name => "LANGUAGESETS_CREATE", label => "Create language sets?" },
    { name => "LANGUAGESETS_MODIFY", label => "Modify language sets?" },
    { name => "LANGUAGESETS_REMOVE", label => "Remove language sets?" },
    { name => "TEMPLATESETS_VIEW", label => "View template sets?" },
    { name => "TEMPLATESETS_CREATE", label => "Create template sets?" },
    { name => "TEMPLATESETS_MODIFY", label => "Modify template sets?" },
    { name => "TEMPLATESETS_REMOVE", label => "Remove template sets?" },
    { name => "BACKUPS_VIEW", label => "View backups?" },
    { name => "BACKUPS_CREATE", label => "Create backups?" },
    { name => "BACKUPS_REMOVE", label => "Remove backups?" },
    { name => "BACKUPS_RESTORE", label => "Restore backups?" },
    { name => "CONVERTDATABASE", label => "Convert database?" },
    { name => "IMPORTEXPORT", label => "Import/Export?" },
    { name => "EMAIL", label => "Email list members?" },
    { name => "EXPIRE", label => "Expire all static pages?" },
    { name => "INSTALLPACKAGE", label => "Install package?" },
    { name => "UPDATE", label => "Update superlinks?" }
  );

  my @Category = (
    { name => "RESOURCES_VIEW", label => "View resources?" },
    { name => "RESOURCES_APPROVE", label => "Approve resources?" },
    { name => "RESOURCES_CREATE", label => "Create resources?" },
    { name => "RESOURCES_MODIFY", label => "Modify resources?" },
    { name => "RESOURCES_REMOVE", label => "Remove resources?" },
    { name => "REVIEWS_VIEW", label => "View reviews?" },
    { name => "REVIEWS_MODIFY", label => "Modify reviews?" },
    { name => "REVIEWS_REMOVE", label => "Remove reviews?" },
  );

  unless ($SL::QUERY{'NotFirst'}) {
    foreach my $key (keys %{ $in{'input'}->{'LEVEL'}->{'permissions'} }) {
      if ($key eq "CATEGORIES") {
        foreach my $category (keys %{ $in{'input'}->{'LEVEL'}->{'permissions'}->{'CATEGORIES'} }) {
          $SL::QUERY{'FORM_CATEGORY_'.$category} = "1";
          foreach my $perm (keys %{ $in{'input'}->{'LEVEL'}->{'permissions'}->{'CATEGORIES'}->{$category} }) {
            $SL::QUERY{'FORM_CATEGORY_'.$category.'_'.$perm} = $in{'input'}->{'LEVEL'}->{'permissions'}->{'CATEGORIES'}->{$category}->{$perm};
          }
        }
      } else {
        $SL::QUERY{'FORM_'.$key} = $in{'input'}->{'LEVEL'}->{'permissions'}->{$key};
      }
    }
  }        

  my %Hiddens = %SL::QUERY;
  
  delete($Hiddens{'CP'});
  delete($Hiddens{'action'});
  delete($Hiddens{'SID'});
  delete($Hiddens{'Referer'});
  delete($Hiddens{'FORM_TITLE'});
  delete($Hiddens{'FORM_SUPERUSER'});
  delete($Hiddens{'SECTION'});
  delete($Hiddens{'ADD_CATEGORY'});

  my @options;
  push(@options, { value => "GENERAL", label => "General Permissions" });
  push(@options, { value => ".", label => "" });
  push(@options, { value => "..", label => "Categories" });
  push(@options, { value => "...", label => "---------------------------" });

  if ($SL::QUERY{'ADD_CATEGORY'} ne "") {
    $SL::QUERY{'FORM_CATEGORY_'.$SL::QUERY{'ADD_CATEGORY'}} = 1;
    $Hiddens{'FORM_CATEGORY_'.$SL::QUERY{'ADD_CATEGORY'}} = 1;
  }
  
  $Hiddens{'DEL_CATEGORY'} = "";
  if ($SL::QUERY{'DEL_CATEGORY'} ne "") {
    foreach my $key (grep(/^FORM_CATEGORY_$SL::QUERY{'DEL_CATEGORY'}(.*)$/, keys %SL::QUERY)) {
      delete($Hiddens{$key});
      delete($SL::QUERY{$key});
      $SL::QUERY{'SECTION'} = "";
    }
  }

  foreach my $key (keys %SL::QUERY) {
    next unless ($key !~ /^FORM_CATEGORY_(.*)_/ && $key =~ /^FORM_CATEGORY_(.*)$/);

    my $category = $in{'input'}->{'CATEGORIES'}->[$in{'input'}->{'CATEGORIES_IX'}->{$1}];
    push(@options, { value => "CATEGORY_".$1, label => $category->{'FULL_TITLE'} });
  }

  $Body .= $Dialog->FormSelectBox(
    name      => "SECTION",
    subject   => "Sections",
    rows      => 5,
    extra     => { onChange => "form.action.value='ModifyLevel';form.submit()" },
    items     => \@options
  );  

  my $value  = qq~<table cellpadding="3" cellspacing="1" border="0" width="100%">~;
     $value .= qq~<tr>~;
     $value .= qq~<td width="100%">~;
  
  @options = ({ value => "", label => "" });
  foreach my $category (@{ $in{'input'}->{'CATEGORIES'} }) {
    push(@options, { value => $category->{'ID'}, label => $category->{'FULL_TITLE'} });
  }
  
  $value .= $Form->SelectBox(
    name    => "ADD_CATEGORY",
    items   => \@options,
    empty   => 1
  );
  
  $value .= qq~</td>~;
  $value .= qq~<td>~;

  $value .= $Form->Button(
    items => [
      { type => "button", value => "Add", extra => { onClick => "form.action.value='ModifyLevel';form.submit();" } }
    ]
  );

  $value .= qq~</td>~;
  $value .= qq~</tr>~;
  $value .= qq~</table>~;

  $Body .= $Dialog->FormTextBox(
    value   => $value,
    subject => "Add Category Line"
  );

  @options = ();
  if ($SL::QUERY{'SECTION'} eq "GENERAL") {
    foreach my $permission (@General) {
      delete($Hiddens{'FORM_'.$permission->{'name'}});
      push(@options, $Form->CheckBox(name => "FORM_".$permission->{'name'}, default => $SL::QUERY{'FORM_'.$permission->{'name'}}, items => [{ value => "1", label => $permission->{'label'} }]));
    }
  } elsif ($SL::QUERY{'SECTION'} =~ /^CATEGORY_(.*)$/) {
    foreach my $permission (@Category) {
      delete($Hiddens{'FORM_CATEGORY_'.$1.'_'.$permission->{'name'}});
      push(@options, $Form->CheckBox(name => "FORM_CATEGORY_".$1."_".$permission->{'name'}, default => $SL::QUERY{'FORM_CATEGORY_'.$1.'_'.$permission->{'name'}}, items => [{ value => "1", label => $permission->{'label'} }]));
    }
    push(@options, $Form->Button(items => [{ type => "button", value => "Delete Category Line", extra => { onClick => "form.DEL_CATEGORY.value='".$1."';form.action.value='ModifyLevel';form.submit();" } }]));
  }

  if (scalar(@options) >= 1) {
    $Body .= $Dialog->FormTextBox(
      value   => join("<br>", @options),
      subject => "Permissions"
    );
  } else {
    $Body .= $Dialog->FormTextBox(
      value   => "Please select a section above to show individual permissions.",
      subject => "Permissions"
    );
  }

  $Body .= $Dialog->FormButton(
    items => [
      { type => "submit", value => "Modify" },
      { type => "reset", value => "Cancel" }
    ], join => "&nbsp;"
  );

  my @Hiddens;
  foreach my $key (keys %Hiddens) {
    push(@Hiddens, { name => $key, value => $Hiddens{$key} });
  }

  $Body = $Dialog->Table(body => $Body);
  $Body = $Dialog->StandardPage(
    body        => $Body,
    header      => "Modify a Level",
    form        => 1,
    head_extra  => "<script src=\"$SYSTEM->{'PUBLIC_URL'}/functions.js\"></script>",
    hiddens     => [
      @Hiddens,
      { name => "NotFirst", value => "1" },
      { name => "Referer", value => $SL::QUERY{'Referer'} },
      { name => "LID", value => $in{'input'}->{'LEVEL'}->{'ID'} },
      { name => "action", value => "DoModifyLevel" },
      { name => "CP", value => "1" },
      { name => "SID", value => $SL::ADMIN{'SID'} }
    ],
    ($in{'input'}->{'LEVEL'}->{'ID'} != $SL::ADMIN{'LEVEL'} ? (toolbar => [
      { type => "divider" },
      { type => "icon", url => "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=DoRemoveLevels&LID=$in{'input'}->{'LEVEL'}->{'ID'}&Referer=".$SL::CGI->escape($SL::QUERY{'Referer'})."&SID=$SL::ADMIN{'SID'}');", icon => "Toolbar/remove.gif", caption => "Remove" }
    ]) : ())
  );
  
  return $Body;
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my %in = (input => undef, @_);

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Dialog = HTML::Dialog->new();

  my $Body = $Dialog->TableRow(items => [{ body => "The following level has been modified:", colspan => 2 }]);

  $Body .= $Dialog->FormTextBox(
    value     => $in{'input'}->{'RECORD'}->{'ID'},
    subject   => "ID"
  );
  
  $Body .= $Dialog->FormTextBox(
    value     => $in{'input'}->{'RECORD'}->{'TITLE'},
    subject   => "Title"
  );

  $Body .= $Dialog->TableDivider(colspan => 2);
  $Body .= $Dialog->TableRow(items => [{ body => "Click <a href=\"".($SL::QUERY{'Referer'} || "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=Levels&SID=$SL::ADMIN{'SID'}")."\">here</a> if you're not redirected in 5 seconds.", colspan => 2 }]);

  $Body = $Dialog->Table(body => $Body);
  $Body = $Dialog->StandardPage(
    body    => $Body,
    header  => "Modify a Level",
    metas   => [
      { http_equiv => "refresh", content => "5; url=".($SL::QUERY{'Referer'} || "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=Levels&SID=$SL::ADMIN{'SID'}") }
    ]
  );
  
  return $Body;
}

1;